package com.apex.lottery.controller.converter;

import com.apex.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.apex.lottery.common.exception.ControllerException;
import com.apex.lottery.controller.result.BaseUserInfoResult;
import com.apex.lottery.controller.result.UserLoginResult;
import com.apex.lottery.controller.result.UserRegisterResult;
import com.apex.lottery.service.dto.UserDTO;
import com.apex.lottery.service.dto.UserLoginDTO;
import com.apex.lottery.service.dto.UserServiceDTO;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName UserConverter
 * @Description controller层转换方法
 * @Author ZJX
 * @Date 2025/4/19 17:22
 * @Version 1.0
 **/
public class UserConverter {
    public static List<BaseUserInfoResult> convertToUserBaseInfoResult(List<UserDTO> userDTOList) {
        if (CollectionUtils.isEmpty(userDTOList)) {
            return List.of();
        }

        return userDTOList.stream()
                .map(userDTO -> {
                    BaseUserInfoResult result = new BaseUserInfoResult();
                    result.setUserId(userDTO.getUserId());
                    result.setUserName(userDTO.getUserName());
                    result.setIdentity(userDTO.getIdentity().name());
                    return result;
                }).collect(Collectors.toList());
    }


    public static UserLoginResult convertToUserLoginResult(UserLoginDTO userLoginDTO) {
        if (null == userLoginDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }

        UserLoginResult result = new UserLoginResult();
        result.setToken(userLoginDTO.getToken());
        result.setIdentity(userLoginDTO.getIdentity().name());

        return result;
    }

    public static UserRegisterResult convertToUserRegisterResult(UserServiceDTO userServiceDTO) {
        UserRegisterResult userRegisterResult = new UserRegisterResult();
        if (null == userRegisterResult){
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }

        userRegisterResult.setUserId(userServiceDTO.getUserId());
        return userRegisterResult;
    }
}
